package com.fd.wpx.authorizating.client.roleresolver;

import com.fd.wpx.authorizating.pojo.PermissionContext;
import com.fd.wpx.authorizating.session.RemoteSessionService;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.permission.RolePermissionResolver;
import org.apache.shiro.authz.permission.WildcardPermission;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

public class CustomRolePermissionResolver implements RolePermissionResolver {
    @Autowired
    private RemoteSessionService sessionService;
    @Override
    public Collection<Permission> resolvePermissionsInRole(String roleString) {
        List result=new ArrayList<>();
        if(roleString.equals("system.admin"))
        {
            Permission permission=new WildcardPermission("system:*:*");
            result.add(permission);
            return result;
        }
        PermissionContext context=sessionService.getPermissionsByRole(roleString);
        Set<String> permissions=context.getPermissions();
        for (String p : permissions){
            Permission permission=new WildcardPermission(p);
            result.add(permission);
        }
        return result;
    }
}
